//
//  BluetoothStateManager.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/6/1.
//


import Foundation
import CoreBluetooth


/// 蓝牙状态处理器
open class BluetoothStateManager: NSObject, CBCentralManagerDelegate {
    
    public typealias BLEStateHandler = (_ state: CBManagerState) -> Void
    
    public static let shared: BluetoothStateManager = .init()
    
    public var bleState: CBManagerState {
        manager?.state ?? .poweredOff
    }
    // MARK: - Manager
    private var manager: CBCentralManager?
    private var monitorStateHandler: BLEStateHandler?
    
    private var onceRequestStateHandler: BLEStateHandler?
    
    private override init() {
        super.init()
    }
    
    /// 监听 BLE 状态
    public func monitorBLEState(_ handler: BLEStateHandler?) {
        monitorStateHandler = handler
        
        if manager == nil {
            manager = CBCentralManager(delegate: self, queue: nil, options: [:])
        } else {
            print("蓝牙开关状态 state = \(manager!.state.string)")

            monitorStateHandler?(manager!.state)
        }
    }
    
    /// 监听 BLE 状态
    public func startMonitorBLEState(_ handler: BLEStateHandler?) {
        monitorBLEState(handler)
    }
    public func stopMonitorBLEState() {
        monitorBLEState(nil)
    }
    
    /// 获取 BLE 状态
    public func requestBLEState(_ onceHandler: BLEStateHandler?) {
        onceRequestStateHandler = onceHandler
        
        if manager == nil {
            manager = CBCentralManager(delegate: self, queue: nil, options: [:])
        } else {
            print("蓝牙开关状态1 state = \(manager!.state.string)")

            onceRequestStateHandler?(manager!.state)
            onceRequestStateHandler = nil
        }
    }
    
    public func centralManagerDidUpdateState(_ central: CBCentralManager) {
        print("蓝牙开关状态 state = \(central.state.string)")
        
        monitorStateHandler?(central.state)
        
        onceRequestStateHandler?(central.state)
        onceRequestStateHandler = nil
    }
    
}


// MARK: - CBManagerState extension
public extension CBManagerState {
    
    // 是否开启
    var isOpen: Bool {
        return self == .poweredOn
    }
    
    // 是否关闭
    var isClose: Bool {
        return self == .poweredOff
    }
    
    /// 是否授权
    var isAuthorized: Bool {
        return self != .unauthorized
    }
    
    /// 是否可用
    var isValid: Bool {
        return (self != .poweredOff) && (self != .unauthorized) && (self != .unsupported)
    }
    
    var string: String {
        switch self {
        case .unknown:
            return "未知"
            
        case .resetting:
            return "重置"
            
        case .unsupported:
            return "不支持"
            
        case .unauthorized:
            return "未授权"
            
        case .poweredOn:
            return "打开"
            
        case .poweredOff:
            return "关闭"
            
        @unknown default:
            return "未知的默认状态"
        }
    }
    
}
